<?php

class OthersController extends Zend_Controller_Action
{
    function preDispatch()
    {
		if(!$_SESSION['identity'])
        {
            $this->_redirect("/login");
        }
		
         //Make menu
         $mod_menu = new Db_Tables2_BDictMenu();
         $this->view->main_menu = $mod_menu->MenuDecolation();
        
        $mod_params = new Db_Tables2_BDictParams();
        $this->view->sys_name = $mod_params->GetSysName();
    }
	
    function indexAction()
    {
        $this->_redirect("/others/profile");
    }
	
	function profileAction()
	{
		$params = $this->_request->getParams();
		
		$this->view->title = "资料修改";
		
		$hxb_db = Zend_Registry::get('hxb_db');
		$select = $hxb_db->select();
		$select->from("auth_emp", array("realName"));
		$select->where("id = ?", $_SESSION['identity']['userid']);
		$row = $hxb_db->fetchRow($select);
		
		if($row)
		{
			$this->view->data = $row;
		}
		
		if(1 == $params['act'])
		{
			$this->view->msg = '更新成功。';
		}elseif(2 == $params['act'])
		{
			$this->view->msg = '更新失败。';
		}
	}
	
	function profileSubmitAction()
	{
		$params = $this->_request->getParams();
		
		$set = array();
		
		if(trim($params['realname']))
		{
			$set['realName'] = trim($params['realname']);
		}
		
		if($params['pwd1'])
		{
			$set['password'] = md5(strtolower($_SESSION['identity']['username']).$params['pwd1']);
		}
			
		if(!empty($set))
		{
			$hxb_db = Zend_Registry::get('hxb_db');
			$where = "id=".$_SESSION['identity']['userid'];
			
			if($hxb_db->update("auth_emp", $set, $where))
			{
				$this->_redirect('/others/profile/act/1');
			}else{
				$this->_redirect('/others/profile/act/2');
			}
		}else{
			$this->_redirect('/others/profile/act/2');
		}
	}
	
    function busAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "BU管理";
		
		$mod_tables = new Db_Joins_TablesJoins();
    	
		$this->view->data = $mod_tables->GetBus();
		
		if($params['status'])
		{
				switch ($params['status'])
				{
					case 1:
						$this->view->msg = "您已成功添加BU";
						break;
					case 2:
						$this->view->msg = "状态更改成功";
						break;
					case 3:
						$this->view->msg = "状态更改失败";
						break;
					case 4:
						$this->view->msg = "关键参数缺失";
						break;
				}
		}
	}
	
	function addBuAction()
	{
		if("LEVEL-3" == $_SESSION['identity']['level'])
		{
			$params = $this->_request->getParams();
			$this->view->title = "新增BU";
			
			$mod_params = new Db_Tables2_BDictParams();
			$roles = $mod_params->GetVal(array("LEVEL-1", "LEVEL-2"));
			
			$roles_arr = $users = array();
			
			foreach($roles as $role)
			{
				$roles_arr[] = $role;
			}
			
			if(!empty($roles_arr))
			{
				$mod_user = new Db_Tables2_User();
				$users = $mod_user->GetUsersByRole($roles_arr);
			}
			
			$this->view->users = $users;
			
			$mod_group = new Db_Tables2_TDealerGroup();
			$this->view->groups = $mod_group->GetGroups();
			
			$mod_factory = new Db_Tables2_TFactory();
			$this->view->factories = $mod_factory->GetFactories();
		
			$mod_t_area = new Db_Tables2_TArea();
			$this->view->cities = $mod_t_area->GetAllCities();

			if($params['error'])
			{
					switch ($params['error'])
					{
						case 1:
							$this->view->msg = "关键参数缺失";
							break;
						case 2:
							$this->view->msg = "写入数据失败";
							break;
						default:
							$this->view->msg = "其他错误";
							break;
					}
			}
		}else{
			echo "无此权限";
			die;
		}
	}
	
	function addBuSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['bu_name'] && $params['emp_id'] && "LEVEL-3" == $_SESSION['identity']['level'])
		{
			$group_id_txt = $city_id_txt = $factory_id_txt = NULL;
			
			if(isset($params['group_id']))
			{
				if(!empty($params['group_id']))
				{
					$group_id_txt = implode("|", $params['group_id']);
				}
			}
			
			if(isset($params['factory_id']))
			{
				if(!empty($params['factory_id']))
				{
					$factory_id_txt = implode("|", $params['factory_id']);
				}
			}
			
			if(isset($params['city_id']))
			{
				if(!empty($params['city_id']))
				{
					$city_id_txt = implode("|", $params['city_id']);
				}
			}
			
			$data = array(
				"bu_name" => $params['bu_name'],
				"emp_id" => $params['emp_id'],
				"status" => 1,
				"ctime" => date("Y-m-d H:i:s")
			);
			
			$dealers_db = Zend_Registry::get('dealers_db');
			
			if($dealers_db->insert("b_bu", $data))
			{
				$data = array(
					"emp_id" => $params['emp_id'],
					"group_id_txt" => $group_id_txt,
					"factory_id_txt" => $factory_id_txt,
					"city_id_txt" => $city_id_txt
				);

				$dealers_db = Zend_Registry::get('dealers_db');

				if($dealers_db->insert("b_bu_auth", $data))
				{
					$this->_redirect("/others/bus/status/1");
				}else{
					$this->_redirect("/others/bus/status/2"); //数据写入失败
				}
			}else{
				$this->_redirect("/others/bus/status/2"); //数据写入失败
			}
		}else{
			$this->_redirect("/others/bus/status/3"); //关键参数缺失
		}
	}
	
	function editBuAction()
	{
		$params = $this->_request->getParams();
		
		if("LEVEL-3" == $_SESSION['identity']['level'] && $params['bu_id'])
		{
			$params = $this->_request->getParams();
			$this->view->title = "编辑BU";
			
			$mod_bu = new Db_Tables2_BBu();
			$bu_info = $mod_bu->GetBuInfo($params['bu_id']);
			$this->view->bu_info = $bu_info;
			
			if(!empty($bu_info))
			{
				$this->view->group_id_arr = explode("|", $bu_info['group_id_txt']);
				$this->view->city_id_arr = explode("|", $bu_info['city_id_txt']);
				$this->view->factory_id_arr = explode("|", $bu_info['factory_id_txt']);
				
				$mod_params = new Db_Tables2_BDictParams();
				$roles = $mod_params->GetVal(array("LEVEL-1", "LEVEL-2"));

				$roles_arr = $users = array();

				foreach($roles as $role)
				{
					$roles_arr[] = $role;
				}

				if(!empty($roles_arr))
				{
					$mod_user = new Db_Tables2_User();
					$users = $mod_user->GetUsersByRole($roles_arr);
				}

				$this->view->users = $users;

				$mod_group = new Db_Tables2_TDealerGroup();
				$this->view->groups = $mod_group->GetGroups();

				$mod_factory = new Db_Tables2_TFactory();
				$this->view->factories = $mod_factory->GetFactories();

				$mod_t_area = new Db_Tables2_TArea();
				$this->view->cities = $mod_t_area->GetAllCities();

				if($params['error'])
				{
						switch ($params['error'])
						{
							case 1:
								$this->view->msg = "关键参数缺失";
								break;
							case 2:
								$this->view->msg = "写入数据失败";
								break;
							default:
								$this->view->msg = "其他错误";
								break;
						}
				}
			}else{
				$this->_redirect("/others/bus/error/1"); //关键参数缺失
			}
		}else{
			echo "无此权限";
			die;
		}
	}
	
	function editBuSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['bu_name'] && $params['emp_id'] && $params['bu_id'] && "LEVEL-3" == $_SESSION['identity']['level'])
		{
			$group_id_txt = $city_id_txt = $factory_id_txt = NULL;
			
			if(isset($params['group_id']))
			{
				if(!empty($params['group_id']))
				{
					$group_id_txt = implode("|", $params['group_id']);
				}
			}
			
			if(isset($params['factory_id']))
			{
				if(!empty($params['factory_id']))
				{
					$factory_id_txt = implode("|", $params['factory_id']);
				}
			}
			
			if(isset($params['city_id']))
			{
				if(!empty($params['city_id']))
				{
					$city_id_txt = implode("|", $params['city_id']);
				}
			}
			
			$data = array(
				"bu_name" => $params['bu_name'],
				"emp_id" => $params['emp_id'],
				"status" => intval($params['status'])
			);
			
			$where = "bu_id=".intval($params['bu_id']);
			
			$dealers_db = Zend_Registry::get('dealers_db');
			
			$dealers_db->update("b_bu", $data, $where);
				
			$data = array(
				"group_id_txt" => $group_id_txt,
				"factory_id_txt" => $factory_id_txt,
				"city_id_txt" => $city_id_txt
			);
			
			$where = "emp_id=".intval($params['emp_id']);

			$dealers_db->update("b_bu_auth", $data, $where);
			$this->_redirect("/others/bus/status/1");
		}else{
			$this->_redirect("/others/bus/error/1"); //关键参数缺失
		}
	}
	
    function myBuAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "下级权限管理";
		
		$emp_id = 0;
		
		if($params['bu_id'] && "LEVEL-3" == $_SESSION['identity']['level'])
		{
			$this->dealers_db = Zend_Registry::get("dealers_db");
			
			$select = $this->dealers_db->select();
			$select->from("b_bu", array("emp_id"));
			$select->where("bu_id = ?", $params['bu_id']);
			$data = $this->dealers_db->fetchRow($select);
			
			if($data)
			{
				$emp_id = $data['emp_id'];
			}
		}else{
			$emp_id = $_SESSION['identity']['userid'];
		}
		
		if($emp_id)
		{
			$mod_bu_auth = new Db_Tables2_BBuAuth();
			$this->view->data = $mod_bu_auth->GetStaffsInfo($emp_id, 1);
		}else{
			echo "关键参数缺失";
			die;
		}
		
		if($params['status'])
		{
				switch ($params['status'])
				{
					case 1:
						$this->view->msg = "您已成功添加下级权限";
						break;
					case 2:
						$this->view->msg = "状态更改成功";
						break;
					case 3:
						$this->view->msg = "状态更改失败";
						break;
				}
		}
	}
	
	function addStaffAction()
	{
		if(1)
		{
			$params = $this->_request->getParams();
			$this->view->title = "新增下级权限";
			
			$mod_users = new Db_Tables2_User();
			$this->view->users = $mod_users->GetMyStaffs($_SESSION['identity']['userid']);
			
			$mod_group = new Db_Tables2_TDealerGroup();
			$this->view->groups = $mod_group->GetGroups();
			
			$mod_factory = new Db_Tables2_TFactory();
			$this->view->factories = $mod_factory->GetFactories();
		
			$mod_t_area = new Db_Tables2_TArea();
			$this->view->cities = $mod_t_area->GetAllCities();
			
			$dealers_db = Zend_Registry::get("dealers_db");
			$select = $dealers_db->select();
			$select->from("b_bu_auth", "*");
			$select->where("emp_id = ?", $_SESSION['identity']['userid']);
			$row = $dealers_db->fetchRow($select);
			
			if($row)
			{
				$this->view->my_group_id_arr = explode("|", $row['group_id_txt']);
				$this->view->my_city_id_arr = explode("|", $row['city_id_txt']);
				$this->view->my_factory_id_arr = explode("|", $row['factory_id_txt']);
			}
			
			if($params['error'])
			{
					switch ($params['error'])
					{
						case 1:
							$this->view->msg = "关键参数缺失";
							break;
						case 2:
							$this->view->msg = "写入数据失败";
							break;
						default:
							$this->view->msg = "其他错误";
							break;
					}
			}
		}else{
			echo "无此权限";
			die;
		}
	}
	
	function addStaffSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['title'] && $params['emp_id'])
		{
			$group_id_txt = $city_id_txt = $factory_id_txt = NULL;
			
			if(isset($params['group_id']))
			{
				if(!empty($params['group_id']))
				{
					$group_id_txt = implode("|", $params['group_id']);
				}
			}
			
			if(isset($params['factory_id']))
			{
				if(!empty($params['factory_id']))
				{
					$factory_id_txt = implode("|", $params['factory_id']);
				}
			}
			
			if(isset($params['city_id']))
			{
				if(!empty($params['city_id']))
				{
					$city_id_txt = implode("|", $params['city_id']);
				}
			}
			
			if(1)
			{
				$data = array(
					"emp_id" => $params['emp_id'],
					"title" => trim($params['title']),
					"group_id_txt" => $group_id_txt,
					"factory_id_txt" => $factory_id_txt,
					"city_id_txt" => $city_id_txt
				);

				$dealers_db = Zend_Registry::get('dealers_db');

				if($dealers_db->insert("b_bu_auth", $data))
				{
					$this->_redirect("/others/my-bu/status/1");
				}else{
					$this->_redirect("/others/my-bu/status/3"); //数据写入失败
				}
			}else{
				$this->_redirect("/others/my-bu/status/3"); //数据写入失败
			}
		}else{
			$this->_redirect("/others/my-bu/status/3"); //关键参数缺失
		}
	}
	
	function editStaffAction()
	{
		$params = $this->_request->getParams();
		
		if($params['emp_id'])
		{
			$params = $this->_request->getParams();
			$this->view->title = "编辑下级权限";
			
			$dealers_db = Zend_Registry::get("dealers_db");
			$select = $dealers_db->select();
			$select->from("b_bu_auth", "*");
			$select->where("emp_id = ?", $params['emp_id']);
			$row = $dealers_db->fetchRow($select);
			
			if($row)
			{
				$this->view->emp_id = $row['emp_id'];
				$this->view->emp_title = $row['title'];
				
				$this->view->group_id_arr = explode("|", $row['group_id_txt']);
				$this->view->city_id_arr = explode("|", $row['city_id_txt']);
				$this->view->factory_id_arr = explode("|", $row['factory_id_txt']);
				
				$mod_users = new Db_Tables2_User();
				$this->view->users = $mod_users->GetMyStaffs($_SESSION['identity']['userid']);

				$mod_group = new Db_Tables2_TDealerGroup();
				$this->view->groups = $mod_group->GetGroups();

				$mod_factory = new Db_Tables2_TFactory();
				$this->view->factories = $mod_factory->GetFactories();

				$mod_t_area = new Db_Tables2_TArea();
				$this->view->cities = $mod_t_area->GetAllCities();
				
				$select = $dealers_db->select();
				$select->from("b_bu_auth", "*");
				$select->where("emp_id = ?", $_SESSION['identity']['userid']);
				$row = $dealers_db->fetchRow($select);
				
				if($row)
				{
					$this->view->my_group_id_arr = explode("|", $row['group_id_txt']);
					$this->view->my_city_id_arr = explode("|", $row['city_id_txt']);
					$this->view->my_factory_id_arr = explode("|", $row['factory_id_txt']);
				}

				if($params['error'])
				{
						switch ($params['error'])
						{
							case 1:
								$this->view->msg = "关键参数缺失";
								break;
							case 2:
								$this->view->msg = "写入数据失败";
								break;
							default:
								$this->view->msg = "其他错误";
								break;
						}
				}
			}else{
				$this->_redirect("/others/bus/error/1"); //关键参数缺失
			}
		}else{
			echo "无此权限";
			die;
		}
	}
	
	function editStaffSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['title'] && $params['original_emp_id'])
		{
			$group_id_txt = $city_id_txt = $factory_id_txt = NULL;
			
			if(isset($params['group_id']))
			{
				if(!empty($params['group_id']))
				{
					$group_id_txt = implode("|", $params['group_id']);
				}
			}
			
			if(isset($params['factory_id']))
			{
				if(!empty($params['factory_id']))
				{
					$factory_id_txt = implode("|", $params['factory_id']);
				}
			}
			
			if(isset($params['city_id']))
			{
				if(!empty($params['city_id']))
				{
					$city_id_txt = implode("|", $params['city_id']);
				}
			}
			
			if(1)
			{
				$data = array(
					"title" => trim($params['title']),
					"group_id_txt" => $group_id_txt,
					"factory_id_txt" => $factory_id_txt,
					"city_id_txt" => $city_id_txt
				);
				
				$where = "emp_id=".intval($params['original_emp_id']);

				$dealers_db = Zend_Registry::get('dealers_db');

				if($dealers_db->update("b_bu_auth", $data, $where))
				{
					$this->_redirect("/others/my-bu/status/1");
				}else{
					$this->_redirect("/others/my-bu/status/3"); //数据写入失败
				}
			}else{
				$this->_redirect("/others/my-bu/status/3"); //数据写入失败
			}
		}else{
			$this->_redirect("/others/my-bu/status/3"); //关键参数缺失
		}
	}

    function adminAction() {
        $params = $this->_request->getParams();
        $this->view->title = "用户管理";

        if(1 == $params['msg']) {
            $this->view->msg = "添加成功";
        }elseif(2 == $params['msg']) {
            $this->view->msg = "添加失败：关键参数缺失";
        }elseif(3 == $params['msg']) {
            $this->view->msg = "添加失败：初始错误";
        }elseif(4 == $params['msg']) {
            $this->view->msg = "删除成功";
        }elseif(5 == $params['msg']) {
            $this->view->msg = "编辑成功";
        }

        $hxb_db = Zend_Registry::get('hxb_db');
        $select = $hxb_db->select();
        $select->from("auth_emp as e", array("id", "username", "createTime", "realName", "status", "email"));
        $select->joinLeft("auth_emp_position as p", "p.empId=e.id", array());
        $select->joinLeft("sys_role as r", "r.id=p.positionId", array("name as roleName"));
        $select->order("e.id DESC");
        $rows = $hxb_db->fetchAll($select);

        $this->view->data = $rows;
    }

    function adminEditAction() {
        $params = $this->_request->getParams();
        $this->view->title = "编辑用户";

        if($params['admin_id']) {
            $this->view->admin_id = $params['admin_id'];

            $hxb_db = Zend_Registry::get('hxb_db');
            $select = $hxb_db->select();
            $select->from("auth_emp as e", array("username", "realName", "status", "email"));
            $select->joinLeft("auth_emp_position as p", "p.empId=e.id", array("positionId"));
            $select->where("e.id = ?", $params['admin_id']);
            $row = $hxb_db->fetchRow($select);

            $this->view->data = $row;

            $this->view->title .= ": ".$row['username'];
        }else {
            $this->_redirect("/others/admin/msg/2");
        }
    }

    function adminEditSubmitAction() {
        $params = $this->_request->getParams();

        if($params['admin_id']) {
            $hxb_db = Zend_Registry::get('hxb_db');
            $select = $hxb_db->select();
            $select->from("auth_emp", array("username"));
            $select->where("id = ?", $params['admin_id']);
            $row = $hxb_db->fetchRow($select);

            $set = array(
                "realName" => trim($params['realName']),
                "email" => trim($params['email']),
                "status" => $params['status']
            );

            if(trim($params['pwd'])){
                $set['password'] = md5($row['username'].trim($params['pwd']));
            }

            $where = "id = ".$params['admin_id'];

            $hxb_db->update("auth_emp", $set, $where);

            $set = array(
                "positionId" => $params['positionId']
            );

            $where = "empId = ".$params['admin_id'];

            $hxb_db->update("auth_emp_position", $set, $where);

            $this->_redirect("/others/admin/msg/5");
        }else {
            $this->_redirect("/others/admin/msg/2");
        }
    }

    function adminAddAction() {
        $params = $this->_request->getParams();
        $this->view->title = "添加用户";
    }

    function adminAddSubmitAction() {
        $params = $this->_request->getParams();

        if($params['username'] && $params['pwd']) {
            $hxb_db = Zend_Registry::get('hxb_db');

            $set = array(
                "username" => trim($params['username']),
                "realName" => trim($params['realName']),
                "status" => $params['status'],
                "createTime" => date("Y-m-d H:i:s"),
                "email" => trim($params['email']),
                "password" => md5(trim($params['username']).trim($params['pwd']))
            );

            $hxb_db->insert("auth_emp", $set);

            $select = $hxb_db->select();
            $select->from("auth_emp", array("id"));
            $select->where("username = ?", trim($params['username']));
            $row = $hxb_db->fetchRow($select);

            $empId = $row['id'];
            $positionId = $params['positionId'];

            $set = array(
                "empId" => $empId,
                "positionId" => $positionId
            );

            $hxb_db->insert("auth_emp_position", $set);

            $this->_redirect("/others/admin/msg/1");
        }else{
            $this->_redirect("/others/admin/msg/2");
        }

        $this->_redirect("/others/admin/msg/3");
    }

    function productManagersAction() {
        $params = $this->_request->getParams();
        $this->view->title = "执行主管配置";

        if(1 == $params['msg']) {
            $this->view->msg = "添加成功";
        }elseif(2 == $params['msg']) {
            $this->view->msg = "添加失败：关键参数缺失";
        }elseif(3 == $params['msg']) {
            $this->view->msg = "添加失败：初始错误";
        }elseif(4 == $params['msg']) {
            $this->view->msg = "删除成功";
        }
        
        $mod_params = new Db_Tables2_BDictParams();
        $sys_id = $mod_params->GetSysId();

        $dealers_db = Zend_Registry::get('dealers_db');
        $select = $dealers_db->select();
        $select->from("dealers_db".$sys_id.".b_mail_list as l", array("id", "is_primary", "status"));
        $select->joinLeft("hxb_db".$sys_id.".auth_emp as e", "e.id=l.emp_id", array("username", "realName"));
        $select->joinLeft("dealers_db".$sys_id.".b_dict_product as p", "p.product_id=l.product_id", array("product_name"));
        $select->order("l.product_id");
        $rows = $dealers_db->fetchAll($select);

        $this->view->data = $rows;
    }

    function productManagersAddAction() {
        $params = $this->_request->getParams();
        $this->view->title = "绑定新规则";

        $products_arr = $managers_arr = array();

        $dealers_db = Zend_Registry::get('dealers_db');
        $select = $dealers_db->select();
        $select->from("b_dict_product", array("product_id", "product_name"));
        $select->where("status = ?", 1);
        $rows = $dealers_db->fetchAll($select);

        foreach($rows as $row)
        {
            $products_arr[$row['product_id']] = $row['product_name'];
        }

        $hxb_db = Zend_Registry::get('hxb_db');
        $select = $hxb_db->select();
        $select->from("auth_emp as e", array("id", "username", "realName"));
        $select->joinLeft("auth_emp_position as p", "p.empId=e.id", array());
        $select->where("p.positionId = ?", 49);
        $select->where("e.status = ?", 1);
        $rows = $hxb_db->fetchAll($select);

        foreach($rows as $row)
        {
            $managers_arr[$row['id']] = array(
                "username" => $row['username'],
                "realName" => $row['realName']
            );
        }

        $this->view->products_arr = $products_arr;
        $this->view->managers_arr = $managers_arr;
    }

    function productManagersAddSubmitAction() {
        $params = $this->_request->getParams();

        if($params['product_id'] && $params['emp_id']) {
            $dealers_db = Zend_Registry::get('dealers_db');

            $set = array(
                "product_id" => $params['product_id'],
                "emp_id" => $params['emp_id'],
                "is_primary" => $params['is_primary'],
                "status" => $params['status']
            );

            $dealers_db->insert("b_mail_list", $set);

            $this->_redirect("/others/product-managers/msg/1");
        }else{
            $this->_redirect("/others/product-managers/msg/2");
        }

        $this->_redirect("/others/product-managers/msg/3");
    }

    function productManagersDelAction() {
        $params = $this->_request->getParams();

        if($params['id']) {
            $where = "id = ".$params['id'];
            $dealers_db = Zend_Registry::get('dealers_db');
            $dealers_db->delete("b_mail_list", $where);

            $this->_redirect("/others/product-managers/msg/4");
        }else {
            $this->_redirect("/others/product-managers/msg/2");
        }
    }
}